package com.skyz.wrap.http;

import android.app.Activity;
import android.app.Application;

import com.hjq.toast.ToastUtils;
import com.skyz.base.application.BaseApplication;
import com.skyz.base.http.HttpRequestCallback;
import com.skyz.base.manager.ActivityManager;
import com.skyz.wrap.entity.result.CommAppJsonResult1;
import com.skyz.wrap.manager.RouteManager;
import com.skyz.wrap.manager.TokenManager;

public abstract class CommonAppHttpRequestCallback1<DATA> extends HttpRequestCallback<CommAppJsonResult1<DATA>> {

    private final String TAG = getClass().getSimpleName();

    protected CommonAppHttpRequestCallback1(boolean showLoading) {
        super(showLoading);
    }

    @Override
    protected void onHttpRequestSuccess(CommAppJsonResult1<DATA> data) {
        if (data.getCode() != 200) {
            if (data.getCode() == 401) {
                ToastUtils.show("登录失效，请重新登录");
                Application application = BaseApplication.getApplication();
//                UserInfoManager.getInstance().setUserInfo(application, null);
                TokenManager.getInstance().setToken(application, null);
                Activity activity = ActivityManager.getInstance().currentActivity();
                if (activity == null) {
                    return;
                }
                ActivityManager.getInstance().finishAllActivity();
                RouteManager.getInstance().showActivity(activity, "WelcomeActivity");
                return;
            } else if (data.getCode() == 101 || data.getCode() == 102) {
                onCommonAppHttpRequestFail(data);
                return;
            }
            onCommonAppHttpRequestFail(data.getMessage());
            return;
        }
        onCommonAppHttpRequestSuccess(data.getToken());
    }

    @Override
    protected void onHttpRequestFail(Throwable e) {
        onCommonAppHttpRequestFail(e.getMessage());
    }

    protected abstract void onCommonAppHttpRequestSuccess(DATA data);

    protected void onCommonAppHttpRequestFail(String msg) {
        ToastUtils.show(msg);
    }

    protected void onCommonAppHttpRequestFail(CommAppJsonResult1<DATA> data) {

    }
}
